package com.movie.util;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import java.util.UUID;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
import java.util.List;


public class RandomDataGenerator {

    private static final String DATA_DIR = "src/main/resources/data/";
    private static final String MOVIES_FILE = DATA_DIR + "movies.txt";
    private static final String SCREENINGS_FILE = DATA_DIR + "screenings.txt";
    private static final String TICKETS_FILE = DATA_DIR + "tickets.txt";
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
    private static final Random random = new Random();


    public static void main(String[] args) {
        generateRandomData();
    }

    public static void generateRandomData() {
        try {
            generateRandomMovies(500);
            generateRandomScreenings();
            generateRandomTickets(1000);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void generateRandomMovies(int numMovies) throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(MOVIES_FILE))) {
            for (int i = 0; i < numMovies; i++) {
                String id = UUID.randomUUID().toString();
                String title = generateRandomMovieTitle();
                String director = generateRandomDirector(); // 使用随机导演名
                String genre = getRandomGenre();
                int duration = random.nextInt(90) + 90;
                writer.write(String.format("%s,%s,%s,%s,%d\n", id, title, director, genre, duration));
            }
        }
    }


    public static void generateRandomScreenings() throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(SCREENINGS_FILE))) {
            try (Stream<String> lines = Files.lines(Paths.get(MOVIES_FILE))) {
                lines.forEach(line -> {
                    String[] parts = line.split(",");
                    if (parts.length > 0) {
                        String movieId = parts[0];
                        int numScreenings = random.nextInt(4) + 2; // 每个电影 2-5 个放映场次
                        try {
                            for (int i = 0; i < numScreenings; i++) {
                                String id = UUID.randomUUID().toString();
                                LocalDateTime startTime = getRandomDateTime();
                                double price = random.nextDouble() * 20 + 5;
                                int seatsAvailable = random.nextInt(100) + 20;
                                writer.write(String.format("%s,%s,%s,%.2f,%d\n", id, movieId, startTime.format(formatter), price, seatsAvailable));
                            }

                        } catch (IOException e) {
                            System.err.println("Error writing screening: " + e.getMessage());
                        }
                    }

                });
            }
        }
    }


    public static void generateRandomTickets(int numTickets) throws IOException {
         List<String> screenings = Files.readAllLines(Paths.get(SCREENINGS_FILE));

         try (BufferedWriter writer = new BufferedWriter(new FileWriter(TICKETS_FILE))) {
              for (int i = 0; i < numTickets; i++) {
                    // Randomly select a screening from the loaded list.
                   String screeningLine = screenings.get(random.nextInt(screenings.size()));
                     String[] screeningParts = screeningLine.split(",");
                     if (screeningParts.length > 0) {
                        String screeningId = screeningParts[0];
                        int seatNumber = random.nextInt(100) + 1;
                        String ticketId = UUID.randomUUID().toString();
                        writer.write(String.format("%s,%s,%d\n", ticketId, screeningId, seatNumber));
                     }

                }
         }
    }

    private static String getRandomGenre() {
        String[] genres = {"Action", "Comedy", "Drama", "Sci-Fi", "Romance", "Horror", "Thriller", "Mystery"};
        return genres[random.nextInt(genres.length)];
    }


    private static LocalDateTime getRandomDateTime() {
        long now = System.currentTimeMillis();
        long oneWeekLater = now + 7 * 24 * 60 * 60 * 1000L;
        long randomTime = now + (long) (random.nextDouble() * (oneWeekLater - now));
        return LocalDateTime.ofInstant(java.time.Instant.ofEpochMilli(randomTime), ZoneId.systemDefault());
    }

    private static String generateRandomMovieTitle() {
        String[] prefixes = {"The", "A", "Secret", "Lost", "Night", "Day", "Shadow", "Silent", "Broken", "Forgotten"};
        String[] nouns = {"City", "Forest", "River", "Mountain", "Island", "Journey", "Dream", "Heart", "World", "Star"};
        String[] suffixes = {"Awakening", "Quest", "Mystery", "Saga", "Chronicles", "Legacy", "Tale", "Odyssey", "Code", "Empire"};
        return prefixes[random.nextInt(prefixes.length)] + " " +
                nouns[random.nextInt(nouns.length)] + " " +
                suffixes[random.nextInt(suffixes.length)];
    }

    private static String generateRandomDirector() {
        String[] firstNames = {"Ava", "Ethan", "Mia", "Noah", "Olivia", "Liam", "Emma", "Jackson", "Sophia", "Aiden"};
        String[] lastNames = {"Smith", "Johnson", "Williams", "Brown", "Jones", "Garcia", "Miller", "Davis", "Rodriguez", "Wilson"};
         return firstNames[random.nextInt(firstNames.length)] + " " + lastNames[random.nextInt(lastNames.length)];
    }
}